package com.founder.android.http;

import java.io.File;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.protocol.HTTP;

import android.content.Context;
import android.os.Handler;

/**
 * HTTP请求包装帮助类
 * @author chen_weihua
 *
 */
public class HttpClientHelper {

    private HttpClientHelper() {}
    
    /**
     * GET方式异步请求字符串
     * @param url
     * @param callback
     */
    public static void asyncGetTextRequest(Context context, String url, ResponseListener callback) {
    	HttpGet get = new HttpGet(url);
    	asyncRequest(context, get, new CallbackWrapper(callback));
    }
    
    /**
     * POST方式异步请求字符串
     * @param url
     * @param params
     * @param callback
     */
    public static void asyncPostTextRequest(Context context, String url, 
    		List<NameValuePair> params, ResponseListener callback) throws Exception {
    	HttpPost post = new HttpPost(url);
    	if (params != null) {
			post.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
		}
    	asyncRequest(context, post, new CallbackWrapper(callback));
    }
    
    /**
     * 异步下载文件到指定File下
     * @param url
     * @param file
     * @param callback
     */
    public static void asyncDownFileRequest(Context context, String url, 
    		File file, ResponseListener callback) {
    	HttpGet get = new HttpGet(url);
    	FileCallbackWapper callbackWapper = new FileCallbackWapper(callback);
    	callbackWapper.setFile(file);
    	asyncRequest(context, get, callbackWapper);
    }
    
    /**
     * 异步下载文件到指定File下
     * @param url
     * @param file
     * @param charset
     * @param callback
     */
    public static void asyncDownFileRequest(Context context, String url, 
    		File file, String charset, ResponseListener callback) {
    	HttpGet get = new HttpGet(url);
    	FileCallbackWapper callbackWapper = new FileCallbackWapper(callback);
    	callbackWapper.setFile(file);
    	callbackWapper.setCharset(charset);
    	asyncRequest(context, get, callbackWapper);
    }
    
    /**
     * 异步请求图片
     * @param url
     * @param callback
     */
    public static void asyncGetImageRequest(Context context, String url, ResponseListener callback) {
    	HttpGet get = new HttpGet(url);
    	asyncRequest(context, get, new ImageCallbackWapper(callback));
    }
    
    /**
     * 异步请求输入流
     * @param url
     * @param callback
     */
    public static void asyncGetStreamRequest(Context context, String url, ResponseListener callback) {
    	HttpGet get = new HttpGet(url);
    	asyncRequest(context, get, new StreamCallbackWapper(callback));
    }
    
    /**
     * 异步请求输入流
     * @param url
     * @param charset
     * @param callback
     */
    public static void asyncGetStreamRequest(Context context, String url, String charset, ResponseListener callback) {
    	HttpGet get = new HttpGet(url);
    	StreamCallbackWapper callbackWapper = new StreamCallbackWapper(callback);
    	callbackWapper.setCharset(charset);
    	asyncRequest(context, get, callbackWapper);
    }
    
    private static void asyncRequest(Context context, HttpUriRequest request, CallbackWrapper callback) {
    	new AsynchronousSender(context, request, new Handler(), callback).start();
    }
}
